layui.use(["layer", "table", "miniTab"], function () {
  // 2020-03-25 HF sessionStorage中取出数据
  var token = sessionStorage.getItem("newtoken");

  var PicMsgRequest = JSON.parse(sessionStorage.getItem("PicMsgRequest"));

  var $ = layui.jquery,
    miniTab = layui.miniTab,
    table = layui.table;

  miniTab.listen();

  $("#Myloading2").remove();
  /*************曲线图初始化*******************/
  var beginTime1 = $("#beginTime").val();
  var endTime1 = $("#endTime").val();

  if (beginTime1 == "" && endTime1 == "") {
    function add0(m) {
      return m < 10 ? "0" + m : m;
    } //避免少0
    // 当前时间
    var time = new Date();
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var date = time.getDate();
    var hours = time.getHours();
    var minutes = time.getMinutes();
    var seconds = time.getSeconds();
    var s2 =
      year +
      "-" +
      add0(month) +
      "-" +
      add0(date) +
      " " +
      add0(hours) +
      ":" +
      add0(minutes) +
      ":" +
      add0(seconds);
    var yesterday = new Date(time - 3000 * 60 * 60 * 24);
    var y = yesterday.getFullYear();
    var m = yesterday.getMonth() + 1;
    var d = yesterday.getDate();
    var s1 =
      y +
      "-" +
      add0(m) +
      "-" +
      add0(d) +
      " " +
      add0(0) +
      ":" +
      add0(0) +
      ":" +
      add0(0);
    // console.log(s1);
    $("#beginTime").val(s1);
    $("#endTime").val(s2);

    // 实时更新当前时间
    var cltime = setInterval(function () {
      let nowtime = new Date();
      var s1 = GMTToStr(nowtime);
      $("#endTime").val(s1);
    }, 1000);

    // 初始化为当前一天的时间
    function getArresterTime() {
      var beginTime1 = $("#beginTime").val();
      var endTime1 = $("#endTime").val();
      $.ajax({
        // url: "/oms-web/map/arrester/time-list?token=" + token,
        url:
          "/oms-web/map/arrester/msg/time-list?token=" +
          token +
          "&terminalNo=" +
          PicMsgRequest.termNumber +
          "&beginTime=" +
          beginTime1 +
          "&endTime=" +
          endTime1,
        dataType: "json",
        type: "GET",
        contentType: "application/json",
        success: function (data) {
          // console.log(data);
          if (data.code === 0) {
            index = layer.load(1);
            layer.close(index);
            writeList(data);
          }
        },
        error: function () {
          layer.msg("数据请求错误,请检查时间格式！");
          layer.close(index);
        },
      });
    }
    console.log(getArresterTime());
  }
  // 左边时间列表
  function writeList(data) {
    var html_txt = "";
    $("#timeList").empty(html_txt);
    for (var i = 0; i < data.data.length; i++) {
      // console.log(data.data[i]);
      html_txt = "<li  style='margin:20px;'>" + data.data[i] + "</li>";
      // html_txt = "<ul>" + "<li>" + data.data[i] + "</li>" + "</ul>";
      // console.log(html_txt);
      // 填充文本
      $("#timeList").append(html_txt);
    }
    // 点击左侧时间，触发曲线
    var time = document.getElementById("timeList").getElementsByTagName("li");
    if (time.length == 0) {
      $("#empty").show();
      $("#ArrTable").hide();
    } else {
      $("#empty").hide();
      $("#ArrTable").show();
    }
    for (var i = 0; i < time.length; i++) {
      time[i].onclick = function () {
        // console.log(this.firstChild.nodeValue);
        var needTime = this.firstChild.nodeValue;
        getHcurveData(needTime);
      };
    }
  }

  // 获取时间 请求数据
  $("#CurveSeach").click(function () {
    var beginTime = $("#beginTime").val();
    var endTime = $("#endTime").val();
    // 判断时间非空
    if (beginTime != "" && endTime != "") {
      function getDaysByDateString(dateString1, dateString2) {
        var startDate = Date.parse(dateString1);
        var endDate = Date.parse(dateString2);
        var diffDate = endDate - startDate + 1 * 24 * 60 * 60 * 1000; //
        var days = parseInt(diffDate / (1 * 24 * 60 * 60 * 1000));
        return days;
      }
      // 验证查询时间   不能过大和过小
      days = getDaysByDateString(beginTime, endTime);
      if (days > 180) {
        layer.msg("错误！查询时间段不能大于180天");
        return false;
      }
      if (days < 0 || days == 0) {
        layer.msg("错误！开始时间必须小于结束时间");
        return false;
      }
      index = layer.load(1);
      // getHcurveData(beginTime, endTime);
      getArresterTime(beginTime, endTime);
    } else {
      layer.msg("请填写开始时间 -- 结束时间！");
    }
    // console.log(beginTime, endTime);
  });

  //2020-03-27 HF  请求报文接口
  function getHcurveData(needTime) {
    $.ajax({
      url:
        "/oms-web/map/arrester/msg?token=" +
        token +
        "&time=" +
        needTime +
        "&terminalNo=" +
        PicMsgRequest.termNumber,
      dataType: "json",
      type: "get",
      contentType: "application/json",
      success: function (data) {
        // console.log(data.data);
        showData(data.data);
      },
      error: function () {
        layer.msg("数据请求错误,请检查时间格式！");
        layer.close(index);
      },
    });
  }
  function showData(res) {
    table.render({
      elem: "#ArresterMsgTableId",
      data: res,
      contentType: "application/json",
      // toolbar: "#toolbarDemo",
      defaultToolbar: [
        "filter",
        "exports",
        "print",
        {
          title: "提示",
          layEvent: "LAYTABLE_TIPS",
          icon: "layui-icon-tips",
        },
      ],

      cols: [
        [
          {
            type: "numbers",
            width: 120,
            title: "序号",
            align: "center",
            fixed: true,
          },
          {
            field: "devName",
            minWidth: 120,
            title: "名称",
            align: "center",
          },
          {
            field: "terminalNo",
            minWidth: 110,
            title: "终端序号",
            align: "center",
          },
          {
            field: "time",
            width: 200,
            title: "时间",
            align: "center",
          },
          {
            field: "message",
            width: 200,
            title: "内容",
            align: "center",
          },
        ],
      ],
      limits: [10, 15, 20, 25, 50, 100],
      limit: 15,
      page: true,
      done: function (res, curr, count) {
        //如果是异步请求数据方式，res即为你接口返回的信息 curr当期页码 count得到数据总量
        if (res.data.length == 0) {
          curr--;
          table.reload({
            page: {
              curr: curr, //重新从第 减1 页开始
            },
          });
        }
      },
    });
  }
  //获取时间函数
  function GMTToStr(time) {
    function add0(m) {
      return m < 10 ? "0" + m : m;
    } //避免少0
    var time = new Date(time);
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var date = time.getDate();
    var hours = time.getHours();
    var minutes = time.getMinutes();
    var seconds = time.getSeconds();
    return (
      year +
      "-" +
      add0(month) +
      "-" +
      add0(date) +
      " " +
      add0(hours) +
      ":" +
      add0(minutes) +
      ":" +
      add0(seconds)
    );
  }

  $("#beginTime").datetimepicker({
    language: "zh-CN", //显示中文
    format: "yyyy-mm-dd hh:ii:ss", //显示格式
    // minView: "month",//设置只显示到月份
    initialDate: new Date(), //初始化当前日期
    autoclose: true, //选中自动关闭
    todayBtn: true, //显示今日按钮
    language: "zh-CN",
  });
  $("#endTime")
    .datetimepicker({
      language: "zh-CN", //显示中文
      format: "yyyy-mm-dd hh:ii:ss", //显示格式
      // minView: "month",//设置只显示到月份
      initialDate: new Date(), //初始化当前日期
      autoclose: true, //选中自动关闭
      todayBtn: true, //显示今日按钮
      // minView:'month',
      language: "zh-CN",
    })
    .on("changeDate", function (ev) {
      window.clearInterval(cltime);
    });
});
